# coding:utf-8
"""
Author: r.xw
Date: 2023/12/9
File: __init__.py.py
"""

import logging
import os
import pathlib
from logging.handlers import RotatingFileHandler
from app.config.config import config
from app.settings import env


# 增加日志配置
def set_log():
    # 日志等级
    logging.basicConfig(level=config[env].LOG_LEVEL, encoding='UTF-8')

    root_path = pathlib.Path(__file__).parent.parent
    log_path = root_path / 'log'

    # 日志记录器, 保存路径、日志大小、保存个数
    file_log_handler = RotatingFileHandler(log_path / "mumunote.log", maxBytes=1024 * 1024 * 300, backupCount=10)

    # 创建日志格式
    formatter = logging.Formatter("%(asctime)s:%(levelname)s:%(filename)s:%(lineno)d %(message)s")
    file_log_handler.setFormatter(formatter)

    # 为全局的日志工具对象添加日志记录器
    logging.getLogger().addHandler(file_log_handler)


set_log()
